<template>
  <div class="register-container">
    <el-form
      ref="formName"
      :model="userInfo"
      :rules="registerRules"
      class="register-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">修改界面</h3>
      </div>

      <!-- 账号 -->
      <el-form-item prop="userName">
        <span class="svg-container">
          <i style="font-size:20px;" class="el-icon-user-solid"></i>
        </span>
        <el-input
          ref="userName"
          v-model="userInfo.userName"
          placeholder="userName"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 密码框 -->
      <el-form-item prop="userPassword">
        <span class="svg-container">
          <i style="font-size:20px;" class="el-icon-lock"></i>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="userInfo.userPassword"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleRegister"
        />
        <span class="show-pwd" @click="showPwd">
          <img style="width:25px;vertical-align: middle;" v-if="passwordType" src="./images/闭眼睛.png"/>
          <img style="width:25px;vertical-align: middle;" v-else src="./images/眼睛_显示.png" alt />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;font-weight:600;"
        @click.native.prevent="RegisterUser"
      >注册</el-button>

      <div class="tips">
        <a style="color:red;margin-left:200px;cursor: pointer;" @click="$router.push('/login')">已有账号，返回登录</a>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "EditPassword",
  data() {
    // 自定义密码判断
    const userPassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 16) {
        callback(new Error("长度为 6-16 位字符"));
      } else {
        callback();
      }
    };
    return {
      // 表单信息
      userInfo: {
        userName: "admin",
        userPassword: "112233"
      },
      // 表单约束
      registerRules: {
        userName: [
          { required: true, message:'请输入账号', trigger: "blur" }
        ],
        userPassword: [
          { required: true,message:'请输入密码', trigger: "blur" },
          { validator: userPassword, trigger: "blur"}
        ]
      },
      // 控制提交按钮时否还在加载状态
      loading: false,
      // 定义密码的类型（是否显示）
      passwordType: "password",
      redirect: undefined
    };
  },
  methods: {
    // 查看密码
    showPwd() {
      // 判断密码显示与隐藏来回切换
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      // 点击完后自动获取焦点
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 用户注册
    RegisterUser() {
      this.$refs.formName.validate(async isTrue => {
        if(isTrue){
          try {
            let result = await this.$store.dispatch("studentAdminAccount/getUserRegister",this.userInfo);
            if (result.code === 200) {
              this.$message({
                message: "^_^,注册成功",
                type: "success"
              })
              this.$router.push("/login");
            }
          } catch (error) {
            this.$message.error(`*_*,${error.message}`);
          }
        }else{
          return false
        }
      })
    }
  }
};
</script>

<style lang="less">
@bg: #283443;
@light_gray: #fff;
@cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: @cursor)) {
  .register-container .el-input input {
    color: @cursor;
    
  }
}

/* reset element-ui css */
.register-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 0px;
      color: @light_gray;
      height: 47px;
      caret-color: @cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px @bg inset !important;
        -webkit-text-fill-color: @cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="less" scoped>
@bg: #2d3a4b;
@dark_gray: #889aa4;
@light_gray: #eee;

.register-container {
  // min-height: 100%;
  height: 100vh;
  width: 100%;
  background-color: @bg;
  overflow: hidden;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: @light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
